package com.datamation.swadeshi.control.data;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;







import com.datamation.swadeshi.model.FTranDet;
import com.datamation.swadeshi.model.fDaynPrdDet;

public class fDaynPrdDetDS {
	private SQLiteDatabase dB;
	private DatabaseHelper dbHelper;
	Context context;
	
	private String TAG="swadeshi";
	
	
	public fDaynPrdDetDS (Context context){
		this.context = context;
		dbHelper = new DatabaseHelper(context);
	}
	
	public void open() throws SQLException {
		dB = dbHelper.getWritableDatabase();
	}
	
	
	/*
	 * insert code
	 */
	@SuppressWarnings("static-access")
	public int createOrUpdateNonPrdDet(ArrayList<fDaynPrdDet>  list) 
	{
		
		//int count =0;
		int serverdbID =0;
		
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}
		Cursor cursor = null;
		
	try{
		
    for (fDaynPrdDet nondet : list) {
    		ContentValues values = new ContentValues();
    		
    		String selectQuery = "SELECT * FROM " + dbHelper.TABLE_NONPRDDET 
					+ " WHERE " + dbHelper.NONPRDDET_REFNO+ " = '" +nondet.getNONPRDDET_REFNO()+ "'" + " AND " + dbHelper.NONPRDDET_DEBCODE+ " = '"+nondet.getNONPRDDET_DEBCODE()+"'"   ;
			cursor = dB.rawQuery(selectQuery, null);
    		
    		//values.put(dbHelper.NONPRDDET_ID, 	nondet.getNONPRDDET_ID());
			values.put(dbHelper.NONPRDDET_REFNO, 	nondet.getNONPRDDET_REFNO());
			values.put(dbHelper.NONPRDDET_TXNDATE, 	nondet.getNONPRDDET_TXNDATE());
			values.put(dbHelper.NONPRDDET_DEBCODE, 	nondet.getNONPRDDET_DEBCODE());
			values.put(dbHelper.NONPRDDET_REASON, 	nondet.getNONPRDDET_REASON());
			values.put(dbHelper.NONPRDDET_LONGITUDE,nondet.getNONPRDDET_LONGITUDE());
			values.put(dbHelper.NONPRDDET_LATITUDE, nondet.getNONPRDDET_LATITUDE());
			values.put(dbHelper.NONPRDDET_REPCODE, 	nondet.getNONPRDDET_REPCODE());
			values.put(dbHelper.NONPRDDET_IS_SYNCED,nondet.getNONPRDDET_IS_SYNCED());
									

			
			int count = cursor.getCount();
			if(count>0)
			{
				serverdbID = dB.update(dbHelper.TABLE_NONPRDDET, values,dbHelper.NONPRDDET_REFNO + " =?" + " AND " + dbHelper.NONPRDDET_DEBCODE + " =?",	
						new String[] { String.valueOf(nondet.getNONPRDDET_REFNO()),String.valueOf(nondet.getNONPRDDET_DEBCODE())});
				
			}else
			{
				serverdbID = (int) dB.insert(dbHelper.TABLE_NONPRDDET, null, values);
			}

			
			
			
			
			
			}
		}catch (Exception e) {
		
			Log.v(TAG+" Exception", e.toString());
	
		}finally {  
			if (cursor !=null) {
				cursor.close();
			}
			dB.close();
		}
		return serverdbID;
				
	}
	
	public ArrayList<fDaynPrdDet> getAllnonprdDetails(String refno) {
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
		
		ArrayList<fDaynPrdDet> list = new ArrayList<fDaynPrdDet>();
		
		String selectQuery = "select * from "+dbHelper.TABLE_NONPRDDET +" WHERE "+dbHelper.NONPRDDET_REFNO+"='"+refno+"'";
		
		Cursor cursor = dB.rawQuery(selectQuery, null);
		while(cursor.moveToNext()){
			
			fDaynPrdDet fnonset=new fDaynPrdDet();
		
			fnonset.setNONPRDDET_DEBCODE(cursor.getString(cursor.getColumnIndex(dbHelper.NONPRDDET_DEBCODE)));
			fnonset.setNONPRDDET_REASON(cursor.getString(cursor.getColumnIndex(dbHelper.NONPRDDET_REASON)));
			fnonset.setNONPRDDET_ID(cursor.getString(cursor.getColumnIndex(dbHelper.NONPRDDET_ID)));
	
							
			list.add(fnonset);
			
		}
		
		return list;
	}
	public String getDuplicate(String code,String RefNo)
	{
		
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
		
		String selectQuery = "SELECT * FROM " + dbHelper.TABLE_NONPRDDET +" WHERE "+dbHelper.NONPRDDET_DEBCODE+"='"+code+"' AND "+dbHelper.NONPRDDET_REFNO+"='"+RefNo+"'" ;
		
		Cursor cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext()){

			return cursor.getString(cursor.getColumnIndex(dbHelper.NONPRDDET_DEBCODE));
			
		}
			
		return "";
	}
	@SuppressWarnings("static-access")
	public int deleteOrdDetByID(String id){
		
		int count =0;
		
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}
		Cursor cursor = null;
		try{
			
			cursor = dB.rawQuery("SELECT * FROM " + dbHelper.TABLE_NONPRDDET +" WHERE "+dbHelper.NONPRDDET_ID+"='"+id+"'", null);
			count =cursor.getCount();
			if(count>0)
			{
				int success = dB.delete(dbHelper.TABLE_NONPRDDET, dbHelper.NONPRDDET_ID+"='"+id+"'", null);
				Log.v("FtranDet Deleted ", success+"");
			}
		}catch (Exception e){

			Log.v(TAG+" Exception", e.toString());
			
		}finally{  
			if (cursor !=null) {
				cursor.close();
			}
			dB.close();
		}
		
		return count;
		
	}
	
	@SuppressWarnings("static-access")
	public int OrdDetByID(String RefNo){
		
		int count =0;
		
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}
		Cursor cursor = null;
		try{
			
			cursor = dB.rawQuery("SELECT * FROM " + dbHelper.TABLE_NONPRDDET +" WHERE "+dbHelper.NONPRDDET_REFNO+"='"+RefNo+"'", null);
			count =cursor.getCount();
			if(count>0)
			{
				int success = dB.delete(dbHelper.TABLE_NONPRDDET, dbHelper.NONPRDDET_REFNO+"='"+RefNo+"'", null);
				Log.v("FtranDet Deleted ", success+"");
			}
		}catch (Exception e){

			Log.v(TAG+" Exception", e.toString());
			
		}finally{  
			if (cursor !=null) {
				cursor.close();
			}
			dB.close();
		}
		
		return count;
		
	}
	
}
